use serde::Deserialize;

#[derive(Deserialize)]
pub struct WebConfig {
    pub addr: String,
}

#[derive(Deserialize)]
pub struct DBConfig {
    pub dsn: String,
    pub max_conns: u32,
}

#[derive(Deserialize)]
pub struct CacheConfig {
    pub dsn: String,
    pub max_conns: u32,
    pub key_prefix: String,
    pub timeout: u32,
}

#[derive(Deserialize)]
pub struct HCaptchaConfig {
    pub secret_key: String,
    pub site_key: String,
    pub request_timeout: u32,
}
#[derive(Deserialize)]
pub struct JwtConfig {
    pub secret_key: String,
    pub expired: u32,
    pub sub: String,
}

#[derive(Deserialize)]
pub struct Config {
    pub web: WebConfig,
    pub db: DBConfig,
    pub cache: CacheConfig,
    pub hcaptcha: HCaptchaConfig,
    pub admin_jwt: JwtConfig,
    pub user_jwt: JwtConfig,
}

impl Config {
    pub fn from_env() -> Result<Self, config::ConfigError> {
        config::Config::builder()
            .add_source(config::Environment::default())
            .build()?
            .try_deserialize()
    }
}
